package com.hengtongxing.hejiayun_employee.base;

import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.gyf.immersionbar.ImmersionBar;
import com.hengtongxing.hejiayun_employee.R;
import com.hengtongxing.hejiayun_employee.appconst.Constants;

import org.greenrobot.eventbus.EventBus;

public class BaseFragment extends Fragment implements SimpleImmersionOwner {

    private Context context;
    public BaseActivity mActivity;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mActivity = (BaseActivity) context;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    public void registerEventBus() {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }


    /**
     * @param activity : 将要被显示的Activity
     */
    public void skipToActivity(Class<? extends BaseActivity> activity) {
        ((BaseActivity) context).showActivity(activity, null);
    }

    /**
     * @param activity : 将要被显示的Activity
     */
    public void skipToActivity(Class<? extends BaseActivity> activity, Bundle data) {
        ((BaseActivity) context).showActivity(activity, data);
    }


    /**
     * 处理权限
     */
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Constants.PERMISSION_REQUEST_CODE) {
            handlerPermissionForActivityResult();
            return;
        }
    }

    public void handlerPermissionForActivityResult() {

    }

    public void forward(Fragment fragment, boolean hide) {
        forward(getId(), fragment, null, hide);
    }

    public void forward(int viewId, Fragment fragment, String name, boolean hide) {
        FragmentTransaction trans = getFragmentManager().beginTransaction();
        if (hide) {
            trans.hide(this);
            trans.add(viewId, fragment);
        } else {
            trans.replace(viewId, fragment);
        }

        trans.addToBackStack(name);
        trans.commitAllowingStateLoss();
    }

    public void backward() {
        getFragmentManager().popBackStack();
    }

    /**
     * 是否登录
     */
//    public boolean isNeedLogin() {
//        String token = SPManager.getInstance().getToken();
//        Intent intent = null;
//        if (TextUtils.isEmpty(token)) {
//            intent = new Intent(mActivity, MobileLoginActivity.class);
//            startActivity(intent);
//            return true;
//        }
//        return false;
//    }

    /**
     * ImmersionBar代理类
     */
    private SimpleImmersionProxy mSimpleImmersionProxy = new SimpleImmersionProxy(this);

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        mSimpleImmersionProxy.setUserVisibleHint(isVisibleToUser);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mSimpleImmersionProxy.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        //注销eventbus
        EventBus.getDefault().unregister(this);
        mSimpleImmersionProxy.onDestroy();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        mSimpleImmersionProxy.onHiddenChanged(hidden);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mSimpleImmersionProxy.onConfigurationChanged(newConfig);
    }

    @Override
    public void initImmersionBar() {
        //白
        ImmersionBar.with(this)
                .statusBarDarkFont(false)
                .navigationBarDarkIcon(true)
                .navigationBarColor(R.color.white)
                .statusBarDarkFont(false, 0.2f)
                .init();
        //黑
//        ImmersionBar.with(this)
//                .statusBarDarkFont(true)
//                .navigationBarDarkIcon(true)
//                .navigationBarColor(R.color.white)
//                .statusBarDarkFont(true, 0.2f)
//                .init();
    }


    @Override
    public boolean immersionBarEnabled() {
        return true;
    }

    public boolean isDestroy() {
        boolean isDestroy = false;
        if (mActivity == null || mActivity.isFinishing()) {
            isDestroy = true;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            if (mActivity.isDestroyed()) {
                isDestroy = true;
            }
        }
        return isDestroy;
    }

}
